package com.rssreader.client.mvc.views;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.rssreader.client.components.ItemPanel;
import com.rssreader.client.mvc.controllers.ItemController;
import com.rssreader.client.mvc.events.AppEvents;
import com.rssreader.shared.model.Item;

public class ItemView extends View {
    private final ItemPanel itemPanel = new ItemPanel();

    public ItemView(ItemController controller) {
	super(controller);
    }

    @Override
    protected void handleEvent(AppEvent event) {
	EventType eventType = event.getType();
	if (eventType.equals(AppEvents.Init)) {
	    Dispatcher
		    .forwardEvent(new AppEvent(AppEvents.ItemPanelReady, itemPanel));
	} else if (eventType.equals(AppEvents.ItemSelected)) {
	    onItemSelected(event);
	}
    }

    private void onItemSelected(AppEvent event) {
	Item item = (Item) event.getData();
	itemPanel.displayItem(item);
    }
}
